"use strict";

/*
 * MikoPBX - free phone system for small business
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/* global globalTranslate, SemanticLocalization, PbxApi, globalRootUrl, IndexSoundPlayer*/

/**
 * Object representing sound files table.
 *
 * @module soundFiles
 */
var soundFiles = {
  $audioFilesList: $('#custom-sound-files-table, #moh-sound-files-table'),
  $tabMenuItems: $('#sound-files-menu .item'),

  /**
   * Initializes the sound files table.
   */
  initialize: function initialize() {
    // Initialize tab menu with history tracking
    soundFiles.$tabMenuItems.tab({
      history: true,
      historyType: 'hash'
    }); // Initialize DataTable for audio files list

    soundFiles.$audioFilesList.DataTable({
      lengthChange: false,
      paging: false,
      columns: [null, {
        orderable: false,
        searchable: false
      }, {
        orderable: false,
        searchable: false
      }],
      order: [0, 'asc'],
      initComplete: function initComplete() {
        // Initialize IndexSoundPlayer for each file row
        $('.file-row').each(function (index, row) {
          var id = $(row).attr('id');
          return new IndexSoundPlayer(id);
        });
      },
      language: SemanticLocalization.dataTableLocalisation
    });
    soundFiles.dataTable = soundFiles.$audioFilesList.DataTable();
    soundFiles.dataTable.on('draw', function () {
      // Reinitialize IndexSoundPlayer for each file row after DataTable redraw
      $('.file-row').each(function (index, row) {
        var id = $(row).attr('id');
        return new IndexSoundPlayer(id);
      });
    }); // Move the "Add New" button to the first eight-column div

    $('#add-new-custom-button').appendTo($('#custom-sound-files-table_wrapper div.eight.column:eq(0)'));
    $('#add-new-moh-button').appendTo($('#moh-sound-files-table_wrapper div.eight.column:eq(0)')); // Add error event listener to audio elements

    var toArray = Array.prototype.slice;
    toArray.apply(document.getElementsByTagName('audio')).forEach(function (audio) {
      audio.addEventListener('error', soundFiles.handleMediaError);
    }); // Add click event listener to delete links

    $('body').on('click', 'a.delete', function (e) {
      e.preventDefault();
      var fileName = $(e.target).closest('tr').attr('data-value');
      var fileId = $(e.target).closest('tr').attr('id');
      PbxApi.FilesRemoveAudioFile(fileName, fileId, soundFiles.cbAfterDelete);
    });
  },

  /**
   * Callback after successful file deletion.
   * @param {string} id - The ID of the deleted file.
   * @returns {boolean}
   */
  cbAfterDelete: function cbAfterDelete(id) {
    $('.message.ajax').remove();
    $.api({
      url: "".concat(globalRootUrl, "sound-files/delete/").concat(id),
      on: 'now',
      successTest: function successTest(response) {
        // test whether a JSON response is valid
        return response !== undefined && Object.keys(response).length > 0;
      },
      onSuccess: function onSuccess(response) {
        if (response.success === true) {
          soundFiles.$audioFilesList.find("tr[id=".concat(id, "]")).remove();
          sessionStorage.removeItem("".concat(globalRootUrl, "sound-files/getSoundFiles/custom"));
          sessionStorage.removeItem("".concat(globalRootUrl, "sound-files/getSoundFiles/moh"));
        } else {
          UserMessage.showMultiString(response.message);
        }
      }
    });
  },

  /**
   * Handles media errors.
   * @param {Event} e - The error event.
   */
  handleMediaError: function handleMediaError(e) {
    switch (e.target.error.code) {
      case e.target.error.MEDIA_ERR_ABORTED:
        console.log('You aborted the media playback.');
        break;

      case e.target.error.MEDIA_ERR_NETWORK:
        console.log('A network error caused the media download to fail.');
        break;

      case e.target.error.MEDIA_ERR_DECODE:
        console.log('The media playback was aborted due to a corruption problem or because the media used features your browser did not support.');
        break;

      case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
        console.log('The media could not be loaded, either because the server or network failed or because the format is not supported.');
        break;

      default:
        console.log('An unknown media error occurred.');
    }

    var $row = $(e.target).closest('tr');
    $row.addClass('negative');
    $row.find('td.player').html(globalTranslate.sf_FileNotFound);
  }
}; // When the document is ready, initialize the sound files table

$(document).ready(function () {
  soundFi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hdWRpbyBmaWxlcyBsaXN0XG4gICAgICAgIHNvdW5kRmlsZXMuJGF1ZGlvRmlsZXNMaXN0LkRhdGFUYWJsZSh7XG4gICAgICAgICAgICBsZW5ndGhDaGFuZ2U6IGZhbHNlLFxuICAgICAgICAgICAgcGFnaW5nOiBmYWxzZSxcbiAgICAgICAgICAgIGNvbHVtbnM6IFtcbiAgICAgICAgICAgICAgICBudWxsLFxuICAgICAgICAgICAgICAgIHtvcmRlcmFibGU6IGZhbHNlLCBzZWFyY2hhYmxlOiBmYWxzZX0sXG4gICAgICAgICAgICAgICAge29yZGVyYWJsZTogZmFsc2UsIHNlYXJjaGFibGU6IGZhbHNlfSxcbiAgICAgICAgICAgIF0sXG4gICAgICAgICAgICBvcmRlcjogWzAsICdhc2MnXSxcbiAgICAgICAgICAgIGluaXRDb21wbGV0ZSgpIHtcbiAgICAgICAgICAgICAgICAvLyBJbml0aWFsaXplIEluZGV4U291bmRQbGF5ZXIgZm9yIGVhY2ggZmlsZSByb3dcbiAgICAgICAgICAgICAgICAkKCcuZmlsZS1yb3cnKS5lYWNoKChpbmRleCwgcm93KSA9PiB7XG4gICAgICAgICAgICAgICAgICAgIGNvbnN0IGlkID0gJChyb3cpLmF0dHIoJ2lkJyk7XG4gICAgICAgICAgICAgICAgICAgIHJldHVybiBuZXcgSW5kZXhTb3VuZFBsYXllcihpZCk7XG4gICAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgICB9LFxuICAgICAgICAgICAgbGFuZ3VhZ2U6IFNlbWFudGljTG9jYWxpemF0aW9uLmRhdGFUYWJsZUxvY2FsaXNhdGlvbixcbiAgICAgICAgfSk7XG5cbiAgICAgICAgc291bmRGaWxlcy5kYXRhVGFibGUgPSBzb3VuZEZpbGVzLiRhdWRpb0ZpbGVzTGlzdC5EYXRhVGFibGUoKTtcblxuICAgICAgICBzb3VuZEZpbGVzLmRhdGFUYWJsZS5vbignZHJhdycsICgpID0+IHtcbiAgICAgICAgICAgIC8vIFJlaW5pd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bGljayBldmVudCBsaXN0ZW5lciB0byBkZWxldGUgbGlua3NcbiAgICAgICAgJCgnYm9keScpLm9uKCdjbGljaycsICdhLmRlbGV0ZScsIChlKSA9PiB7XG4gICAgICAgICAgICBlLnByZXZlbnREZWZhdWx0KCk7XG4gICAgICAgICAgICBjb25zdCBmaWxlTmFtZSA9ICQoZS50YXJnZXQpLmNsb3Nlc3QoJ3RyJykuYXR0cignZGF0YS12YWx1ZScpO1xuICAgICAgICAgICAgY29uc3QgZmlsZUlkID0gJChlLnRhcmdldCkuY2xvc2VzdCgndHInKS5hdHRyKCdpZCcpO1xuICAgICAgICAgICAgUGJ4QXBpLkZpbGVzUmVtb3ZlQXVkaW9GaWxlKGZpbGVOYW1lLCBmaWxlSWQsIHNvdW5kRmlsZXMuY2JBZnRlckRlbGV0ZSk7XG4gICAgICAgIH0pO1xuICAgIH0sXG5cbiAgICAvKipcbiAgICAgKiBDYWxsYmFjayBhZnRlciBzdWNjZXNzZnVsIGZpbGUgZGVsZXRpb24uXG4gICAgICogQHBhcmFtIHtzdHJpbmd9IGlkIC0gVGhlIElEIG9mIHRoZSBkZWxldGVkIGZpbGUuXG4gICAgICogQHJldHVybnMge2Jvb2xlYW59XG4gICAgICovXG4gICAgY2JBZnRlckRlbGV0ZShpZCkge1xuICAgICAgICAkKCcubWVzc2FnZS5hamF4JykucmVtb3ZlKCk7XG4gICAgICAgICQuYXBpKHtcbiAgICAgICAgICAgIHVybDogYCR7Z2xvYmFsUm9vdFVybH1zb3VuZC1maWxlcy9kZWxldGUvJHtpZH1gLFxuICAgICAgICAgICAgb246ICdub3cnLFxuICAgICAgICAgICAgc3VjY2Vzc1Rlc3QocmVzcG9uc2UpIHtcbiAgICAgICAgICAgICAgICAvLyB0ZXN0IHdoZXRoZXIgYSBKU09OIHJlc3BvbnNlIGlzIHZhbGlkXG4gICAgICAgICAgICAgICAgcmV0dXJuIHJlc3BvbnNlICE9PSB1bmRlZmluZWRcbiAgICAgICAgICAgICAgICAgICAgJiYgT2JqZWN0LmtleXMocmVzcG9uc2UpLmxlbmd0aCA+IDA7XG4gICAgICAgICAgICB9LFxuICAgICAgICAgICAgb25TdWNjZXNzKHJlc3BvbnNlKSB7XG4gICAgICAgICAgICAgICAgaWYgKHJlc3BvbnNlLnN1Y2Nlc3MgPT09IHRydWUpIHtcbiAgICAgICAgICAgICAgICAgICAgc291bmRGaWxlcy4kYXVkaW9GaWxlc0xpc3QuZmluZChgdHJbaWQ9JHtpZH1dYCkucmVtb3ZlKCk7XG4gICAgICAgICAgICAgICAgICAgIHNlc3Npb25TdG9yYWdlLnJlbW92ZUl0ZW0oYCR7Z2xvYmFsUm9vdFVybH1zb3VuZC1maWxlcy9nZXRTb3VuZEZpbGVzL2N1c3RvbWApO1xuICAgICAgICAgICAgICAgICAgICBzZXNzaW9uU3RvcmFnZS5yZW1vdmVJdGVtKGAke2dsb2JhbFJvb3RVcmx9c291bmQtZmlsZXMvZ2V0U291bmRGaWxlcy9tb2hgKTtcbiAgICAgICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgICAgICBVc2VyTWVzc2FnZS5zaG93TXVsdGlTdHJpbmcocmVzcG9uc2UubWVzc2FnZSk7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSxcbiAgICAgICAgfSk7XG4gICAgfSxcblxuICAgIC8qKlxuICAgICAqIEhhbmRsZXMgbWVkaWEgZXJyb3JzLlxuICAgICAqIEBwYXJhbSB7RXZlbnR9IGUgLSBUaGUgZXJyb3IgZXZlbnQuXG4gICAgICovXG4gICAgaGFuZGxlTWVkaWFFcnJvcihlKSB7XG4gICAgICAgIHN3aXRjaCAoZS50YXJnZXQuZXJyb3IuY29kZSkge1xuICAgICAgICAgICAgY2FzZSBlLnRhcmdldC5lcnJvci5NRURJQV9FUlJfQUJPUlRFRDpcbiAgICAgICAgICAgICAgICBjb25zb2xlLmxvZygnWW91IGFib3J0ZWQgdGhlIG1lZGlhIHBsYXliYWNrLicpO1xuICAg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